iT邦幫忙

DAY 23
0

學習技術筆記系列 第 18

Day23[C++]左右為難的小偷

  • 分享至 

  • xImage
  •  

第三十四屆ITSA 第四題


#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>

using namespace std;

class item {
public:
string name;
float value;
float weight;

item():value(0),weight(0) {}
item(const string &s,const float &v,const float &w)
:name(s),value(v),weight(w) {}
};

const float MAX_SIZE = 20.0;

int main() {

item pack[5];
int spForPack = 0;
string n;
float v;
float w;

float totalWeight = 0.0;

for(int r0 = 0;r0 < 5;++r0) {
cin>>n>>v>>w;

const item newItem(n,v,w);

if((totalWeight + newItem.weight) > MAX_SIZE) {
item ground[5];
int spForGround = 0;

spForPack -= 1;

while(spForPack >= 0) {
const item itemHasHand = pack[spForPack--];
const float weightOfRemove = totalWeight - itemHasHand.weight;

if(
(newItem.value > itemHasHand.value) &&
((weightOfRemove + newItem.weight) <= MAX_SIZE)
) {

// put back
spForPack += 1;
spForGround -= 1;
while(spForGround >= 0) {
pack[spForPack++] = ground[spForGround--];
}

pack[spForPack++] = newItem;

totalWeight = 0;
for(int r0 = 0;r0 < spForPack;++r0) {
totalWeight += pack[r0].weight;
}

break;
}
else {
ground[spForGround++] = itemHasHand;
}
}

// put back
if(spForGround > 0) {
// reset sp
spForPack += 1;
spForGround -= 1;
while(spForGround >= 0) {
pack[spForPack++] = ground[spForGround--];
}
}
}
else {
// put new item to pack
pack[spForPack++] = newItem;
totalWeight += newItem.weight;
}
}

float totalValue = 0;
spForPack -= 1;

while(spForPack >= 0) {

cout<<pack[spForPack].name<<" "
<<pack[spForPack].value<<" "
<<pack[spForPack].weight<<endl;

printf(
"%s %0.1f %0.1f\n",
pack[spForPack].name,
pack[spForPack].value,
pack[spForPack].weight
);

totalValue += pack[spForPack].value;
--spForPack;
}

printf("weight:%0.1f\n",totalWeight);
printf("value:%0.1f\n",totalValue);

system("pause");
}


上一篇
Day22[C++]切割字串
下一篇
Day24[C++]static stack
系列文
學習技術筆記22
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言